struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if (list1 == NULL)
    {
        return list2;
    }
    if (list2 == NULL)
    {
        return list1;
    }
    struct ListNode* head = NULL, * tail = NULL;
    struct ListNode* cur1 = list1, * cur2 = list2;
    while (cur1 && cur2)
    {
        if (cur1->val > cur2->val)
        {
            if (head == NULL)
            {
                head = tail = cur2;
            }
            else
            {
                tail->next = cur2;
                tail = tail->next;
            }
            cur2 = cur2->next;
        }
        else
        {
            if (head == NULL)
            {
                head = tail = cur1;
            }
            else
            {
                tail->next = cur1;
                tail = tail->next;
            }
            cur1 = cur1->next;
        }
    }

    if (cur1 == NULL)
    {
        tail->next = cur2;
    }
    else
    {
        tail->next = cur1;
    }
    return head;
}



int kthToLast(struct ListNode* head, int k) {
    struct ListNode* cur = head;
    int len = 1;
    while (cur)
    {
        cur = cur->next;
        len++;
    }
    struct ListNode* str = head;
    int num = len - k;
    while (--num)
    {
        str = str->next;
    }
    return str->val;
}



struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* slow = head, * fast = head;
    if (head == NULL)
    {
        return NULL;
    }
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}


struct ListNode* reverseList(struct ListNode* head) {
    if (head == NULL)
    {
        return NULL;
    }
    struct ListNode* cur = head;
    struct ListNode* ne = cur->next;
    struct ListNode* prev = NULL;
    while (cur)
    {
        cur->next = prev;
        prev = cur;
        cur = ne;
        if (ne)
            ne = ne->next;
    }
    return prev;
}